﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.ScheduleJob.Dao
{
    public class DataFormatUtil<T>
    {
        public static T DateTimeFormat(T data)
        {
            System.Reflection.PropertyInfo[] properties = data.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            if (properties != null && properties.Length > 0)
            {
                foreach (System.Reflection.PropertyInfo item in properties)
                {
                    if (item.PropertyType == typeof(DateTime))
                    {
                        object value = item.GetValue(data, null);
                        if (value != null)
                        {
                            item.SetValue(item.Name, FormatDateTime(Convert.ToDateTime(value)));
                        }
                    }
                    else if (item.PropertyType == typeof(Metadata.Entity.ScheduleJob.ScheduleTrigger))
                    {
                        object trigger = item.GetValue(data,null);
                        if (trigger != null)
                        {
                            trigger = DataFormatUtil<Metadata.Entity.ScheduleJob.ScheduleTrigger>.DateTimeFormat(trigger as Metadata.Entity.ScheduleJob.ScheduleTrigger);
                            item.SetValue(item.Name, trigger);
                        }
                    }
                }
            }
            return data;
        }

        private static DateTime FormatDateTime(DateTime curDateTime)
        {
            return Convert.ToDateTime(curDateTime.ToString("yyyy/MM/dd HH:mm:ss"));
        }
    }
}
